package com.zhuiyun.project.api.venuemanagement.service.impl;


import com.zhuiyun.project.api.system.user.entity.SysUser;
import com.zhuiyun.project.api.system.user.entity.SysUserRole;
import com.zhuiyun.project.api.venuemanagement.entity.VenueManagement;
import com.zhuiyun.project.api.venuemanagement.mapper.VenueManagementMapper;
import com.zhuiyun.project.api.venuemanagement.model.VenueAddModel;
import com.zhuiyun.project.api.venuemanagement.model.VenueUpdateModel;
import com.zhuiyun.project.api.venuemanagement.service.VenueManagementService;
import com.zhuiyun.project.common.errorCode.CommonException;
import com.zhuiyun.project.common.errorCode.EmErrorCode;
import com.zhuiyun.project.common.response.CommonResult;
import com.zhuiyun.project.utils.BeanUtils;
import com.zhuiyun.project.utils.UploadFileUtils;
import com.zhuiyun.project.utils.pageUtil.MybatisPageHelper;
import com.zhuiyun.project.utils.pageUtil.PageRequest;
import com.zhuiyun.project.utils.pageUtil.PageResult;
import com.zhuiyun.project.utils.uuid.IdUtils;
import com.zhuiyun.project.utils.validator.ValidatorUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.xml.crypto.Data;
import java.util.Date;
import java.util.List;

/**
 * @ClassName CoachManagementServiceImpl
 * @Description TODO
 * @Author wuwan
 * @Date 2023/11/2 13:39
 **/
@Service
public class VenueManagementServiceImpl implements VenueManagementService {
    @Autowired
    private VenueManagementMapper venueManagementMapper;

    @Autowired
    private BCryptPasswordEncoder bCryptPasswordEncoder;
    /*
    * @Author wuwan
    * @Description 添加场馆信息
    * @Date 2023/11/2 14:04
    * @Return
    */
    @Override
    public CommonResult addVenue(VenueAddModel venueAddModel,MultipartFile file) throws CommonException {
        if (ValidatorUtil.ValidationObject(venueAddModel)){
            // 手机号等同于账号
            String venuePhone = venueAddModel.getVenuePhone();
            String encode = bCryptPasswordEncoder.encode(venuePhone);
            String s = UploadFileUtils.uploadFile(file);
            //添加账号密码到登录表
            SysUser sysUser = new SysUser();
            sysUser.setUserName(venueAddModel.getVenueName());
            sysUser.setPhone(venuePhone);
            sysUser.setLoginName(venuePhone);
            sysUser.setPassword(encode);
            sysUser.setAvatarId(s);
            sysUser.setEmail(venueAddModel.getVenueEmail());
            Integer user = venueManagementMapper.addSysUser(sysUser);
            Integer id = sysUser.getId();
            SysUserRole sysUserRole = new SysUserRole();
            sysUserRole.setUserId(Long.valueOf(id));
            venueManagementMapper.addSysUserRole(sysUserRole);
            if (user < 1) {
                return CommonResult.error("账号创建失败");
            }
            VenueManagement venueManagement = new VenueManagement();
            // addModel 对象拷贝成 sysUser
            BeanUtils.copy(venueAddModel, venueManagement);
            // 生成随机数 当做店铺唯一标识符
            venueManagement.setVenueUniqueIdentifier(IdUtils.randomUUID());
            venueManagement.setUsesRoleId(id);
            venueManagement.setVenueEmail(venueAddModel.getVenueEmail());
            venueManagement.setVenueCertificate(s);
            venueManagement.setCreateTime(new Date());
            venueManagement.setUpdateTime(new Date());
            Integer integer = venueManagementMapper.addVenue(venueManagement);
            if (integer >= 1) {
                return CommonResult.ok(EmErrorCode.SAVE_OK);
            }else {
                return CommonResult.ok(EmErrorCode.SAVE_ERROR);
            }
        }else {
            return CommonResult.ok(EmErrorCode.SAVE_ERROR);
        }
    }
    /*
    * @Author wuwan
    * @Description 修改场馆信息
    * @Date 2023/11/3 14:12
    * @Return
    */
    @Override
    public CommonResult updateVenue(VenueUpdateModel venueUpdateModel, MultipartFile file) throws CommonException {
        if (ValidatorUtil.ValidationObject(venueUpdateModel)){
            VenueManagement venueManagement = new VenueManagement();
            // addModel 对象拷贝成 sysUser
            BeanUtils.copy(venueUpdateModel, venueManagement);
            if (file != null) {
                String s = UploadFileUtils.uploadFile(file);
                venueManagement.setVenueCertificate(s);
            }
            venueManagement.setUpdateTime(new Date());

            Integer integer = venueManagementMapper.updateVenue(venueManagement);
            if (integer >= 1) {
                return CommonResult.ok(EmErrorCode.UPDATE_OK);
            }else {
                return CommonResult.ok(EmErrorCode.UPDATE_ERROR);
            }
        }else {
            return CommonResult.ok(EmErrorCode.UPDATE_ERROR);
        }
    }
    /*
    * @Author wuwan
    * @Description 场馆信息分页查询
    * @Date 2023/11/3 14:12
    * @Return
    */
    @Override
    public CommonResult selectVenue(PageRequest pageRequest,String venueName,String venuePhone) {
        MybatisPageHelper.startPage(pageRequest);
        List<VenueManagement> venueManagements = venueManagementMapper.selectVenue(venueName, venuePhone);
        PageResult pageResult = MybatisPageHelper.getPageResult(venueManagements);
        return CommonResult.ok(pageResult);
    }
//    /*
//    * @Author wuwan
//    * @Description 修改场馆状态
//    * @Date 2023/11/6 9:48
//    * @Return
//    */
//    @Override
//    public CommonResult changeUserStatus(VenueManagement venueManagement) {
//        venueManagementMapper.changeUserStatus(venueManagement);
//        return CommonResult.ok();
//    }
@Override
public CommonResult selectById(PageRequest pageRequest, String id) {
    return CommonResult.ok(venueManagementMapper.selectById(pageRequest,id));
}

}

